package UI;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Comp.ChannelCopy;
import Comp.Path_Loader;
import java.awt.Dimension;

public class PopOption extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private final JPanel contentPanel = new JPanel();
	private final String resetSqlpath = "form\\resetDB.sql";
	private final String defaultpath = "C:\\BESS\\resetDB.sql";
	private String mysqlsql = "mysql -u yprima -p1234 bess < c:\\bess\\resetDB.sql";

	private final byte STATE_SAVE = 1;
	private final byte STATE_LOAD = 0;

	/**
	 * Create the dialog.
	 */
	public PopOption(final JFrame mainFrame) {
		super(mainFrame);
		setResizable(false);
		setTitle("Option");
		mainSetting();

		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JButton btnDBReset = new JButton();
			btnDBReset.setPreferredSize(new Dimension(64, 26));
			btnDBReset.setIcon(new ImageIcon("Image/buttion_dbreset"));
			btnDBReset.setBorderPainted(false);
			btnDBReset.setContentAreaFilled(false);
			btnDBReset.setCursor(new Cursor(Cursor.HAND_CURSOR));
			btnDBReset.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						new ChannelCopy(resetSqlpath, defaultpath);
						ProcessBuilder oProcess = new ProcessBuilder("cmd",
								"/c",
								"mysql -u yprima -p1234 bess < c:\\bess\\resetDB.sql");
						oProcess.directory(new File(
								"c:/program files/mysql/mysql server 5.5/bin"));
						Process p = oProcess.start();

						BufferedReader stdOut = new BufferedReader(
								new InputStreamReader(p.getInputStream()));
						BufferedReader stdError = new BufferedReader(
								new InputStreamReader(p.getErrorStream()));
						String s = null;
						while ((s = stdOut.readLine()) != null)
							System.out.println(s);
						while ((s = stdError.readLine()) != null)
							System.err.println(s);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
			contentPanel.add(btnDBReset);
		}
		{
			JButton btnSaveDump = new JButton();
			btnSaveDump.setPreferredSize(new Dimension(84, 26));
			btnSaveDump.setIcon(new ImageIcon("Image/button_export"));
			btnSaveDump.setBorderPainted(false);
			btnSaveDump.setContentAreaFilled(false);
			btnSaveDump.setCursor(new Cursor(Cursor.HAND_CURSOR));
			btnSaveDump.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						Path_Loader PL = new Path_Loader(mainFrame, STATE_SAVE);
						String filename = PL.getfileName();
						String path = PL.getFileDirectory();
						if (filename != null) {
							mysqlsql = "mysqldump -u yprima -p1234 bess > "
									+ path + filename + ".sql";

							ProcessBuilder oProcess = new ProcessBuilder("cmd",
									"/c", mysqlsql);
							oProcess.directory(new File(
									"c:/program files/mysql/mysql server 5.5/bin"));
							Process p = oProcess.start();

							BufferedReader stdOut = new BufferedReader(
									new InputStreamReader(p.getInputStream()));
							BufferedReader stdError = new BufferedReader(
									new InputStreamReader(p.getErrorStream()));
							String s = null;
							while ((s = stdOut.readLine()) != null)
								System.out.println(s);
							while ((s = stdError.readLine()) != null)
								System.err.println(s);
						}
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
			contentPanel.add(btnSaveDump);
		}
		{
			JButton btnLoadDump = new JButton();
			btnLoadDump.setPreferredSize(new Dimension(84, 26));
			btnLoadDump.setIcon(new ImageIcon("Image/button_import"));
			btnLoadDump.setBorderPainted(false);
			btnLoadDump.setContentAreaFilled(false);
			btnLoadDump.setCursor(new Cursor(Cursor.HAND_CURSOR));
			btnLoadDump.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						Path_Loader PL = new Path_Loader(mainFrame, STATE_LOAD);
						String filename = PL.getfileName();
						String path = PL.getFileDirectory();

						if (filename != null) {
							mysqlsql = "mysql -u yprima -p1234 bess < " + path
									+ filename;

							ProcessBuilder oProcess = new ProcessBuilder("cmd",
									"/c", mysqlsql);
							oProcess.directory(new File(
									"c:/program files/mysql/mysql server 5.5/bin"));
							Process p = oProcess.start();

							BufferedReader stdOut = new BufferedReader(
									new InputStreamReader(p.getInputStream()));
							BufferedReader stdError = new BufferedReader(
									new InputStreamReader(p.getErrorStream()));
							String s = null;
							while ((s = stdOut.readLine()) != null)
								System.out.println(s);
							while ((s = stdError.readLine()) != null)
								System.err.println(s);
						}
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
			contentPanel.add(btnLoadDump);
		}

		setVisible(true);
	}

	private void mainSetting() {
		setBounds(100, 100, 400, 88);
		setLocationRelativeTo(null);
		setModal(true);
	}

}
